package com.example.carrierapp.ui.person;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.carrierapp.base.MVPBaseFragment;
import com.example.carrierapp.bean.home.PersonalInfoBean;
import com.example.carrierapp.config.ConstValue;
import com.example.carrierapp.ui.dialog.DialogUtils;
import com.example.carrierapp.ui.person.modifypass.ModifyPassActivity;
import com.example.carrierapp.ui.person.modifyphone.ModifyPhoneActivity;
import com.example.carrierapp.ui.person.notice.NoticeActivity;
import com.example.carrierapp.ui.person.set.SettingActivity;
import com.example.carrierapp.ui.view.CircleImageView;
import com.example.carrierapp.util.ActivityUtil;
import com.example.carrierapp.util.GlideApp;
import com.example.carrierapp.util.SPUtils;
import com.example.carrierapp.util.ToastUtils;
import com.example.lb.fingerdemo.R;
import com.q42.android.scrollingimageview.ScrollingImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PersonCenterFragment extends MVPBaseFragment<PersonCenterContract.View, PersonCenterPrenter> implements PersonCenterContract.View {
    @BindView(R.id.civ_person_head)
    CircleImageView mCiHead;
    @BindView(R.id.tv_person_name)
    TextView mTvName;
    @BindView(R.id.tv_person_phone)
    TextView mTvPhone;
    @BindView(R.id.btn_person_noti)
    Button mBtnNotice;
    @BindView(R.id.img_wave)
    ScrollingImageView mIvWave;

    private String mHeadUrl;

    public static PersonCenterFragment newInstance() {
        PersonCenterFragment homeFragment = new PersonCenterFragment();
        return homeFragment;
    }

    @Override
    protected PersonCenterPrenter createPresenter() {
        return new PersonCenterPrenter(getActivity());
    }

    @Override
    public void initView() {
        ButterKnife.bind(this, mRootView);
    }

    @Override
    public void initData() {
        mPresenter.attachView(this);
        mPresenter.getUserInfo();
        mIvWave.start();
    }

    @Override
    public void getUserInfoSuccess(PersonalInfoBean info) {
//        mHeadUrl = info.headPortrait;
//        GlideApp.with(this).load(info.headPortrait).placeholder(R.mipmap.img_defaultavatar).into(mCiHead);
        mTvName.setText(TextUtils.isEmpty(info.carrierName) ? "承运商" : info.carrierName);
        mTvPhone.setText(TextUtils.isEmpty(info.phone) ? (TextUtils.isEmpty(info.account)?"暂无电话" :info.account): info.phone);
//        if (info.noticeCount > 99) {
//            mBtnNotice.setText("99+");
//            mBtnNotice.setVisibility(View.VISIBLE);
//        } else if (info.noticeCount > 0) {
//            mBtnNotice.setText(info.noticeCount + "");
//            mBtnNotice.setVisibility(View.VISIBLE);
//        } else {
//            mBtnNotice.setVisibility(View.INVISIBLE);
//        }
    }

    @Override
    public void getUserInfoField(String msg) {
        ToastUtils.showCenterToast(getContext(), msg);
    }

    @OnClick({R.id.tv_set_quit,R.id.tv_person_set, R.id.tv_person_noti, R.id.civ_person_head, R.id.tv_person_num, R.id.tv_person_pass})
    public void onCLIck(View v) {
        switch (v.getId()) {
            case R.id.tv_set_quit:
                new DialogUtils.Builder(getContext())
                        .setMessage("确定要退出登录？")
                        .setPositiveButton(null, null, new DialogUtils.CallbackListener() {
                            @Override
                            public void onClick(View v) {
                                SPUtils.putBoolean(getContext(), ConstValue.LOGIN_STATE, false);
                                SPUtils.putString(getContext(), ConstValue.URL_TOKEN, "");
                                getActivity().finish();
                                ActivityUtil.startLogin(getContext(), true);
                            }
                        }).creat().show();
                break;
            case R.id.tv_person_set:
                ActivityUtil.startActivity(getContext(), SettingActivity.class);
                break;
            case R.id.tv_person_noti:
                ActivityUtil.startActivity(getContext(), NoticeActivity.class);
                break;
            case R.id.civ_person_head:
                if (!TextUtils.isEmpty(mHeadUrl))
                    ActivityUtil.startSeePhoto(getContext(), mHeadUrl);
                break;
            case R.id.tv_person_pass:
                if(!mTvPhone.getText().toString().trim().equals("暂无电话"))
                {
                    ActivityUtil.startModifyPass(getContext(),mTvPhone.getText().toString().trim());
                }else
                {
                    ToastUtils.showCenterToast(getContext(),"没有电话号码，不能修改密码");
                }
                break;
            case R.id.tv_person_num:
                if(!mTvPhone.getText().toString().trim().equals("暂无电话"))
                {
                    ActivityUtil.startModifyPhone(getContext(),mTvPhone.getText().toString().trim());
                }else
                {
                    ToastUtils.showCenterToast(getContext(),"没有电话号码，不能修改密码");
                }
                break;
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_person_center;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mIvWave.stop();
    }
}
